package the.offer.DataStructure.Impl;

import the.offer.DataStructure.Interface.Stack;

public class ArrayStack<E extends Comparable> implements Stack<E> {
    Array<E> arrayData;
    Array<E> arrayMin;

    public ArrayStack(int capacity) {
        arrayData = new Array<>(capacity);
        arrayMin = new Array<>(capacity);
    }

    public ArrayStack() {
        arrayData = new Array<>();
        arrayMin = new Array<>();
    }

    @Override
    public void push(E e) {
        arrayData.addLast(e);
        if (arrayMin.isEmpty() || (e.compareTo(arrayMin.getLast()) < 0))
            arrayMin.addLast(e);
    }

    @Override
    public E pop() {
        E res = arrayData.removeLast();
        if (res.compareTo(arrayMin.getLast()) == 0)
            arrayMin.removeLast();
        return res;
    }

    @Override
    public E peek() {
        return arrayData.getLast();
    }

    public E getMin() {
        return arrayMin.getLast();
    }

    @Override
    public int getSize() {
        return arrayData.getSize();
    }

    @Override
    public boolean isEmpty() {
        return arrayData.isEmpty();
    }

    private int getCapacity() {
        return arrayData.getCapacity();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Stack: ");
        res.append('[');
        for (int i = 0; i < arrayData.getSize(); i++) {
            res.append(arrayData.get(i));
            if (i != arrayData.getSize() - 1)
                res.append(",");
        }
        res.append("] top");
        return res.toString();
    }


}
